<template>
  <div class="index-box" ref="index">
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <div class="search" ref="search">
          <el-form :inline="true" :model="searchForm" size="small" ref="searchForm" @submit.native.prevent class="demo-form-inline" id="searchBox">
            <el-form-item prop="telephone">
                <el-input v-model="searchForm.telephone" clearable placeholder="用户手机号"></el-input>
            </el-form-item>
            <el-form-item prop="joinTime">
              <el-date-picker v-model="joinTime" type="daterange" align="right" unlink-panels range-separator="至"
                start-placeholder="加入开始日期" end-placeholder="加入结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>
            <el-form-item prop="orderTime">
              <el-date-picker v-model="orderTime" type="daterange" align="right" unlink-panels range-separator="至"
                start-placeholder="首次下单开始日期" end-placeholder="首次下单结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                :picker-options="pickerOptions">
              </el-date-picker>
            </el-form-item>
            <el-form-item class="clearfix">
              <el-button type="primary" @click="handleSearch()" icon="el-icon-search">搜索</el-button>
              <el-button icon="el-icon-refresh" size="small" @click="resetSearch">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="actionBtn" ref="actionBtn">
          <div class="actionBtnLeft">
            <el-radio-group v-model="searchForm.levelId" size="small" @change="handleSearch">
              <el-radio-button label="">全部</el-radio-button>
              <el-radio-button :label="item.levelId" v-for="item in levelList">{{item.name}}</el-radio-button>
            </el-radio-group>
            <el-button
              style="margin-left:10px"
              type="warning"
              icon="el-icon-download"
              size="small"
              :loading="btnLoading"
              v-has="'statistics:behavior:down'"
              @click="handleExport"
            >导出</el-button>

          </div>
          <div class="actionBtnRight">
              <MyRefresh></MyRefresh>
              <MyTableColumnSet :tableColumn.sync="tableColumn" :hideTableColumn.sync="hideTableColumn" :tableColumnType.sync="tableColumnType"></MyTableColumnSet>
          </div>
        </div>

        <div class="table" ref="table">
          <el-table :data="dataList" style="width: 100%;"  border :height="tableHeight" stripe v-loading="loading">

            <el-table-column prop="userface" label="用户头像" v-if="!hideTableColumn.includes('userface')" width="80" align="center">
              <template #default="scope">
                 <el-image :src="scope.row.userface" fit="fill" v-image-preview="scope.row.userface" class="img"></el-image>
              </template>
            </el-table-column>
            <el-table-column prop="telephone" label="用户手机号" v-if="!hideTableColumn.includes('telephone')" align="center" width="130"></el-table-column>
            <el-table-column prop="nickname" label="用户名" v-if="!hideTableColumn.includes('nickname')" align="center" width="200" show-overflow-tooltip></el-table-column>
            <el-table-column prop="memberLevelName" label="会员等级" v-if="!hideTableColumn.includes('memberLevelName')" align="center" width="120"></el-table-column>
            <el-table-column prop="joinTime" label="加入时间" v-if="!hideTableColumn.includes('joinTime')" align="center" width="130"></el-table-column>
            <el-table-column prop="parentPhone" label="推荐人手机号" v-if="!hideTableColumn.includes('parentPhone')" align="center" width="130"></el-table-column>

            <el-table-column prop="miniProgramCount" label="微信小程序-点击次数" v-if="!hideTableColumn.includes('miniProgramCount')" align="center" width="160"></el-table-column>
            <el-table-column prop="androidCount" label="Android-点击次数(次)" v-if="!hideTableColumn.includes('androidCount')" align="center" width="160"></el-table-column>
            <el-table-column prop="iosCount" label="IOS-点击次数(次)" v-if="!hideTableColumn.includes('iosCount')" align="center" width="120"></el-table-column>
            <el-table-column prop="signCount" label="签到次数(次)" v-if="!hideTableColumn.includes('signCount')" align="center" width="120"></el-table-column>
            <el-table-column prop="shareCount" label="分享次数(次)" v-if="!hideTableColumn.includes('shareCount')" align="center" width="130"></el-table-column>
            <el-table-column prop="shareFansCount" label="分享粉丝人数(人)" v-if="!hideTableColumn.includes('shareFansCount')" align="center" width="130"></el-table-column>
            <el-table-column prop="firstCreateOrderTime" label="首次下单时间" v-if="!hideTableColumn.includes('firstCreateOrderTime')" align="center" width="150"></el-table-column>
            <el-table-column prop="orderActualPayTotal" label="订单实付总金额 (元)" v-if="!hideTableColumn.includes('orderActualPayTotal')" align="center" width="130"></el-table-column>
            <el-table-column prop="unionCount" label="推荐加盟人数(人)" v-if="!hideTableColumn.includes('unionCount')" align="center" width="130"></el-table-column>
            <el-table-column prop="scanStoreCount" label="扫店数(家)" v-if="!hideTableColumn.includes('scanStoreCount')" align="center"
            ></el-table-column>


          </el-table>
        </div>
        <div class="page" ref="page">
           <MyPagination
              v-show="total"
             :total="total"
             :page.sync="searchForm.pageNum"
             :limit.sync="searchForm.pageSize"
             @pagination="dataInfo"
           />
        </div>
      </el-col>
    </el-row>



  </div>
</template>

<script>
  import common from '@/api/api';
  import option from '../../../common/option.js';
  import {
   behaviorStatisticsList,getAllMemberLevel,behaviorStatisticsExport
  } from '@/api/getData'
  export default {
    data() {
      return {
        loading: false,
        btnLoading: false,
        shareTime:[],
        FansJoinTime:[],
        joinTime:[],
        visitTime:[],
        orderTime:[],
        searchForm: {
          telephone:'',
          levelId:'',
          pageNum: 1,
          pageSize: 20,
        },
        total: 0,
        tableColumn:[{/*自定义表格列*/
            name:'用户头像',
            value:'userface'
        },{
            name:'用户手机号',
            value:'telephone'
        },{
            name:'用户名',
            value:'nickname'
        },{
            name:'会员等级',
            value:'memberLevelName'
        },{
            name:'加入时间',
            value:'joinTime'
        },{
            name:'推荐人手机号',
            value:'parentPhone'
        },{
            name:'微信小程序-点击次数',
            value:'miniProgramCount'
        },{
            name:'Android-点击次数(次)',
            value:'androidCount'
        },{
            name:'IOS-点击次数(次)',
            value:'iosCount'
        },{
            name:'签到次数(次)',
            value:'signCount'
        },{
            name:'分享次数(次)',
            value:'shareCount'
        },{
            name:'分享粉丝人数(人)',
            value:'shareFansCount'
        },{
            name:'首次下单时间',
            value:'firstCreateOrderTime'
        },{
            name:'订单实付总金额 (元)',
            value:'orderActualPayTotal'
        },{
            name:'推荐加盟人数(人)',
            value:'unionCount'
        },{
            name:'扫店数(家)',
            value:'scanStoreCount'
        }],
        hideTableColumn:[],/*自定义表格列需要隐藏的*/
        tableColumnType:'staffManagement',/*自定义表格列类型传参*/
        tableHeight:0,//table 高度
        dataList:[],

        multipleSelection: '',
        dialogTitle: '',
        statusRow:{},/*活跃的列*/
        levelList:[]

      }
    },
    computed: {
      pickerOptions: () => {
        return option.pickerOptions;
      }
    },
    mounted() {
      this.dataInfo();
      this.getSelect();
      this.$nextTick(() => {
          this.tableHeight = this.$refs.index.offsetHeight - this.$refs.search.offsetHeight - this.$refs.actionBtn.offsetHeight - this.$refs.page.offsetHeight - 55;
      })
    },
    methods: {
      resetSearch(){
        this.joinTime = [];
        this.orderTime = [];
        this.dataInfo();
      },
      /** 导出按钮操作 */
       handleExport() {
        let data = this.addDateRange(this.searchForm, this.shareTime, 'shareStartTime', 'shareEndTime');
        data = this.addDateRange(this.searchForm, this.FansJoinTime, 'fansJoinStartTime', 'fansJoinEndTime');
        data = this.addDateRange(this.searchForm, this.joinTime, 'joinStartTime', 'joinEndTime');
        data = this.addDateRange(this.searchForm, this.visitTime, 'visitStartTime', 'visitEndTime');
        data = this.addDateRange(this.searchForm, this.orderTime, 'firstCreateOrderStartTime', 'firstCreateOrderEndTime');
         this.$modal.confirm('是否确认导出搜索数据项？').then(() => {
           this.btnLoading = true;
           return behaviorStatisticsExport(data);
         }).then(response => {
           this.$download.name(response.msg);
           this.btnLoading = false;
         }).catch(() => {});
      },
      /** 数据初始化请求动态列表数据 **/
      getSelect(){
        getAllMemberLevel().then(res=>{
          let list = res.data;
          list.map(item=>{
            item.nameMoney = item.levelId!='1'?item.name+"("+item.unionFee+"万元加盟金)":item.name
          })
          this.levelList = list;
        })
      },
      async dataInfo() {
        try {
          let data = this.addDateRange(this.searchForm, this.shareTime, 'shareStartTime', 'shareEndTime');
          data = this.addDateRange(this.searchForm, this.FansJoinTime, 'fansJoinStartTime', 'fansJoinEndTime');
          data = this.addDateRange(this.searchForm, this.joinTime, 'joinStartTime', 'joinEndTime');
          data = this.addDateRange(this.searchForm, this.visitTime, 'visitStartTime', 'visitEndTime');
          data = this.addDateRange(this.searchForm, this.orderTime, 'firstCreateOrderStartTime', 'firstCreateOrderEndTime');
          this.loading = true;
          const result = await behaviorStatisticsList(data);
          if (result.code == 200) {
            this.total = result.total;
            this.dataList = result.rows;
          } else {
            this.$message.error(result.msg);
          }
          this.loading = false;
        } catch (err) {
          this.loading = false;
          console.log(err)
        }
      },

      handleSearch() {
        this.searchForm.pageNum= 1;
        this.dataInfo();
      },

      /** 表格选中状态改变 **/
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },

    }
  }
</script>

<style scoped lang="scss">

  .title{
    text-align: center;
    margin:5px 0;
  }
  .el-table .el-dropdown-link{
      cursor: pointer;
      color: #61997A;
      margin-left: 5px;
  }
    .leftDialog{
      height:540px;
      overflow-y: auto;
    }
    .activeVerticalBox .activeItem{
      color:#61997A;
      cursor: pointer;
      margin: 0px;
      text-align: center;
      height:20px;
      line-height: 20px;
    }
    .activeVerticalBox .el-dropdown{
      display: flex;
      justify-content: center;
      color:#61997A;
      cursor: pointer;
    }
    .activeCheckedBox{
      border-top: 1px solid #ddd;
      padding:10px;
      height:52px;
      .el-tag{
        margin-right:10px;
      }
    }

</style>
